package base;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Random;

public class Source
{
	public LinkedList<Element>	list;
	public int					offset	= 1;	// Allow to prevent 0 weight element
	public int					input	= 0;

	public Source(int seed, int range, int samples)
	{
		generateSource(seed, range, samples);
	}

	/**
	 * Generate {@link Element} for the source
	 * 
	 * @param seed a seed for {@link Random} numbers
	 * @param range a range for {@link Random} numbers
	 * @param samples numbers of {@link Element}
	 */
	public void generateSource(int seed, int range, int samples)
	{
		Random aNumber = new Random(seed);
		list = new LinkedList<Element>();
		for (int i = 0; i < samples; i++)
		{
			list.add(new Element(aNumber.nextInt(range) + offset));
		}
	}

	/**
	 * 
	 * @return source list
	 */
	public LinkedList<Element> getList()
	{
		return list;
	}

	/**
	 * Check if source is empty
	 * 
	 * @return false if not empty
	 */
	public boolean isEmpty()
	{
		return list.isEmpty();
	}

	/**
	 * Push an element from the source in the LoadBalancer
	 * 
	 * @param lb the LoadBalancer
	 */
	public void push(LoadBalancer lb)
	{
		try
		{
			Element el = list.removeFirst();
			el.setInput(input);
			input++;
			lb.load(el);
		}
		catch (NoSuchElementException e)
		{
			System.err.println("No source element to push from loadbalancer");
		}
	}
}
